package com.smart.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.pojo.ResultBean;
import com.smart.pojo.SalesOrder;
import com.smart.req.SalesReq;
import com.smart.resp.SalesOrderResp;
import com.smart.service.ISalesOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lyj
 * @since 2023-05-15
 */
@RestController
@RequestMapping("/sales-order")
public class SalesOrderController {

    @Autowired
    private ISalesOrderService salesOrderService;

    @GetMapping("queryByCondition")
    public ResultBean queryByCondition(SalesReq salesReq) {
        System.out.println("查询销售单的条件：" + salesReq);
        Page<SalesOrderResp> page = new Page<>(salesReq.getPageNum(), salesReq.getPageSize());
        salesOrderService.queryByCondition(salesReq, page);
        return ResultBean.success(page);
    }

    @GetMapping("queryNotOutbound")
    public ResultBean queryNotOutbound() {
        System.out.println("获取为办理出库的销售单");
        List<SalesOrder> list = salesOrderService.lambdaQuery()
                .eq(SalesOrder::getStatus, 0)
                .list();
        return ResultBean.success(list);
    }

    @PostMapping("saveOrUpdate")
    @RequiresPermissions({"sales:save"})
    public ResultBean saveOrUpdate(@RequestBody SalesOrder salesOrder) {
        System.out.println("新增或修改的销售数据：" + salesOrder);
        boolean rs = salesOrderService.saveOrUpdate(salesOrder);
        return rs ? ResultBean.success() : ResultBean.error();
    }

    @DeleteMapping("deleteById/{id}")
    @RequiresPermissions({"sales:del"})
    public ResultBean deleteById(@PathVariable("id") Long id) {
        System.out.println("要删除的销售单id：" + id);
        boolean rs = salesOrderService.removeById(id);
        return rs ? ResultBean.success() : ResultBean.error();
    }
}
